<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Controller\Api\V1;

use App\Middleware\JWTAuthMiddleware;
use App\Services\FriendService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Middlewares([JWTAuthMiddleware::class])]
#[Controller(prefix: '/api/v1/friend/')]
class FriendController extends CController
{
    #[Inject]
    protected FriendService $friendService;

    /**
     * 好友列表.
     */
    #[RequestMapping(path: 'list', methods: 'GET')]
    public function list()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $data = $this->friendService->list($params);
        return $this->success($data, $this->request);
    }

    /**
     * 好友申请列表.
     */
    #[RequestMapping(path: 'apply-list', methods: 'GET')]
    public function applyList()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $data = $this->friendService->applyList($params);
        return $this->success($data, $this->request);
    }

    /**
     * 删除好友申请.
     * @date 2024-05-13 18:18
     * @param mixed $id
     */
    public function deleteApply($id, Request $request): JsonResponse
    {
        $this->friendService->deleteApply($id, $request->user()->id);
        return $this->success([], $request);
    }

    /**
     * 查找好友.
     */
    #[RequestMapping(path: 'search/{keywords}', methods: 'GET')]
    public function search($keywords)
    {
        $params['user'] = $this->user();
        $params['keywords'] = $keywords;
        $data = $this->friendService->search($params);
        return $this->success($data, $this->request);
    }

    /**
     * 好友验证信息.
     */
    #[RequestMapping(path: 'show-confirm', methods: 'post')]
    public function showConfirm()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'source' => 'required|string',
            'relationship' => 'required|string',
            'keywords' => 'required|string',
        ]);
        $data = $this->friendService->showConfirm($params);
        return $this->success($data, $this->request);
    }

    /**
     * 申请添加好友.
     */
    #[RequestMapping(path: 'apply', methods: 'post')]
    public function apply()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'keywords' => 'required|string',
            'nickname' => 'required|string',
            'setting' => 'required|array',
        ]);
        $data = $this->friendService->apply($params);
        return $this->success($data, $this->request);
    }

    /**
     * 通过好友申请.
     */
    #[RequestMapping(path: 'verify', methods: 'post')]
    public function verify()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'friend' => 'required|int',
            'nickname' => 'required|string',
            'setting' => 'required|array',
        ]);
        $data = $this->friendService->verify($params);
        return $this->success($data, $this->request);
    }

    /**
     * 更新朋友设置.
     */
    #[RequestMapping(path: 'update', methods: 'put')]
    public function update()
    {
        $params = $this->request->all();
        $params['user'] = $this->user();
        $this->validate($params, [
            'friend' => 'required|int',
        ]);
        $data = $this->friendService->update($params);
        return $this->success($data, $this->request);
    }
}
